package addon.w3;

import java.util.Vector;

/**
 * Mesh has two pv that parent and own
 * @author madjk
 *
 */
public class MeshBuilder {
	Vector meshGroup;
	
	public MeshBuilder() {
		meshGroup = new Vector();
	}
	
	public void addVector(int gind, float vec[]) {
		Vector tVec = null;
		if(meshGroup.size() <= gind) {
			for(int i=0;i<=gind;i++) {
				if(i==gind) {
					try {
						Object obj = meshGroup.get(i);
						if(obj instanceof Null) {
							meshGroup.add(i, (tVec=new Vector()) );
						} else {
							tVec = (Vector)obj;
						}
					} catch (ArrayIndexOutOfBoundsException e) {
						meshGroup.add(i, (tVec=new Vector()) );
					}
				} else {
					try {
						Object obj = meshGroup.get(i);
					} catch (ArrayIndexOutOfBoundsException e) {
						meshGroup.add(i, new Null());
					}
				}
			}
		}
		tVec.add(vec);
	}
	
	private float[] toFloats(int index) {
		Vector vec = (Vector)meshGroup.get(index);
		float rfs[] = new float[vec.size()*3];
		for(int i=0;i<vec.size();i++) {
			float vector[] = (float[])vec.get(i);
			for(int k=0;k<3;k++) {
				rfs[(3*i)+k] = vector[k];
			}
		}
		return rfs;
	}
		
	public Mesh[][] build(int index[][]) {
		Mesh mesh[][] = new Mesh[index.length][];		
		for(int i=0;i<index.length;i++) {
			mesh[i] = new Mesh[index[i].length];
			for(int j=0;j<index[i].length;j++) {
				mesh[i][j] = new Mesh(toFloats(index[i][j]));
			}
		}
		return mesh;
	}
	
	private int toIndex(int gind) {
		return gind+1;
	}

	public static void main(String args[]) {
//		MeshBuilder.add(1,Vector3f);
//		MeshBuilder.add(2,Vector3f);
	}
}

class Null extends Object {
}

/**
 * Vector group
 * @author madjk
 *
 */
class Mesh {
	float vec[];
	
	public Mesh(float vecs[]) {
		
	}
}